﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GridScrollCircle : BaseScrollCircle
{
    [SerializeField]
    protected GridLayoutGroup.Axis m_DynamicAxis;
    [SerializeField]
    protected RectOffset m_Padding;
    [SerializeField]
    protected Vector2 m_Space;

    [SerializeField]
    protected TextAnchor m_ChildAlignment;

    [SerializeField]
    protected int m_ConstraintCount;

    protected Vector2 cell;//格子大小
    protected Vector2 pltPos;
    protected Vector2 prbPos;
    protected Vector2 cltPos;
    protected Vector2 crbPos;
    protected Vector2 childSize;
    protected Vector2 contentSize;
    protected override void OnRefrashPos(ScrollCircleItem[] items, RectTransform content)
    {
        for (int i = 0; i < items.Length; i++)
        {
            ScrollCircleItem item = items[i];
            Vector3 llb = rt.InverseTransformPoint(content.TransformPoint(new Vector3(item.rect.x - item.rect.width * 0.5f - Mathf.Abs(m_Space.x),
                item.rect.y - item.rect.height * 0.5f - Mathf.Abs(m_Space.y), 0)));
            Vector3 lrt = rt.InverseTransformPoint(content.TransformPoint(new Vector3(item.rect.x + item.rect.width * 1.5f + Mathf.Abs(m_Space.x),
                item.rect.y + item.rect.height * 1.5f + Mathf.Abs(m_Space.y), 0)));          
            Rect itemt = new Rect(llb.x, llb.y, Mathf.Abs(lrt.x - llb.x), Mathf.Abs(lrt.y - llb.y));          
            if (rt.rect.Overlaps(itemt))
                item.OnShow();
            else
                item.OnHide();
        }
    }


    protected override void OnDrawGizmos()
    {
        base.OnDrawGizmos();
        if (IsDraw)
        {
            Gizmos.color = Color.blue;

            Vector2 ll = cltPos;
            Vector2 lr = crbPos;
            Vector3 wl = scrollview.content.TransformPoint(new Vector3(ll.x, ll.y, 0));
            Vector3 wr = scrollview.content.TransformPoint(new Vector3(lr.x, lr.y, 0));

            Gizmos.DrawLine(wl, wl + Vector3.right * Mathf.Abs(wr.x - wl.x));
            Gizmos.DrawLine(wr, wr + Vector3.left * Mathf.Abs(wr.x - wl.x));
            Gizmos.DrawLine(wl, wl + Vector3.down * Mathf.Abs(wr.y - wl.y));
            Gizmos.DrawLine(wr, wr + Vector3.up * Mathf.Abs(wr.y - wl.y));
        }
    }
    protected override void OnRefrashSize(ScrollCircleItem[] items, RectTransform content)
    {
        //取最大宽高
        for (int i = 0; i < items.Length; i++)
        {
            ScrollCircleItem item = items[i];
            Vector2 sz = item.GetSize();
            cell.x = Mathf.Max(cell.x, sz.x);
            cell.y = Mathf.Max(cell.y, sz.y);
        }


        int dCount = (int)(items.Length / m_ConstraintCount) + (items.Length % m_ConstraintCount == 0 ? 0 : 1);
        if (m_DynamicAxis == GridLayoutGroup.Axis.Horizontal)
        {
            childSize.x = cell.x * dCount + (dCount - 1) * m_Space.x;
            childSize.y = cell.y * m_ConstraintCount + (m_ConstraintCount - 1) * m_Space.y;

            contentSize.x = childSize.x + m_Padding.left + m_Padding.right;
            contentSize.y = childSize.y + m_Padding.top + m_Padding.bottom;
            content.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, contentSize.x);
            content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, contentSize.y);
            pltPos.x = content.rect.center.x - content.rect.width / 2.0f;
            pltPos.y = content.rect.center.y + content.rect.height / 2.0f;

            prbPos.x = content.rect.center.x + content.rect.width / 2.0f;
            prbPos.y = content.rect.center.y - content.rect.height / 2.0f;

            cltPos.x = pltPos.x + m_Padding.left;
            cltPos.y = pltPos.y - m_Padding.top;

            crbPos.x = prbPos.x - m_Padding.right;
            crbPos.y = prbPos.y + m_Padding.bottom;
            Vector2 startPos = cltPos;
            switch (m_ChildAlignment)
            {
                case TextAnchor.UpperLeft:
                    startPos = cltPos;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        int ycount = i % m_ConstraintCount;
                        int xcount = (int)((float)i / (float)m_ConstraintCount);// + (ycount > 0 ? 1 : 0);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount, startPos.y + (cell.y + m_Space.y) * ycount));
                    }
                    break;
                case TextAnchor.UpperCenter:
                    startPos.x = cltPos.x + (crbPos.x - cltPos.x) / 2.0f - childSize.x / 2.0f;
                    startPos.y = cltPos.y;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int ycount = i % m_ConstraintCount;
                        int xcount = (int)((float)i / (float)m_ConstraintCount);// + (ycount > 0 ? 1 : 0);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount + (cell.x - sz.x) / 2.0f, startPos.y + (cell.y + m_Space.y) * ycount));
                    }
                    break;
                case TextAnchor.UpperRight:
                    startPos.x = crbPos.x - childSize.x;
                    startPos.y = cltPos.y;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int ycount = i % m_ConstraintCount;
                        int xcount = (int)((float)i / (float)m_ConstraintCount);// + (ycount > 0 ? 1 : 0);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount + (cell.x - sz.x), startPos.y + (cell.y + m_Space.y) * ycount));
                    }
                    break;
                case TextAnchor.LowerLeft:
                    startPos.x = cltPos.x;
                    startPos.y = crbPos.y;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int ycount = i % m_ConstraintCount;
                        int xcount = (int)((float)i / (float)m_ConstraintCount);// + (ycount > 0 ? 1 : 0);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount, startPos.y + (cell.y + m_Space.y) * ycount - (cell.y - sz.y)));
                    }
                    break;
                case TextAnchor.LowerCenter:
                    startPos.x = cltPos.x + (crbPos.x - cltPos.x) / 2.0f - childSize.x / 2.0f;
                    startPos.y = crbPos.y;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int ycount = i % m_ConstraintCount;
                        int xcount = (int)((float)i / (float)m_ConstraintCount);// + (ycount > 0 ? 1 : 0);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount + (cell.x - sz.x) / 2.0f, startPos.y + (cell.y + m_Space.y) * ycount - (cell.y - sz.y)));
                    }
                    break;
                case TextAnchor.LowerRight:
                    startPos.x = crbPos.x - childSize.x;
                    startPos.y = crbPos.y;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int ycount = i % m_ConstraintCount;
                        int xcount = (int)((float)i / (float)m_ConstraintCount);// + (ycount > 0 ? 1 : 0);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount + (cell.x - sz.x), startPos.y + (cell.y + m_Space.y) * ycount - (cell.y - sz.y)));
                    }
                    break;
                case TextAnchor.MiddleLeft:
                    startPos.x = cltPos.x;
                    startPos.y = crbPos.y + (cltPos.y - crbPos.y) / 2.0f - childSize.y / 2.0f;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int ycount = i % m_ConstraintCount;
                        int xcount = (int)((float)i / (float)m_ConstraintCount);// + (ycount > 0 ? 1 : 0);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount, startPos.y + (cell.y + m_Space.y) * ycount - (cell.y - sz.y) / 2.0f));
                    }
                    break;
                case TextAnchor.MiddleCenter:
                    startPos.x = cltPos.x + (crbPos.x - cltPos.x) / 2.0f - childSize.x / 2.0f;
                    startPos.y = crbPos.y + (cltPos.y - crbPos.y) / 2.0f - childSize.y / 2.0f;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int ycount = i % m_ConstraintCount;
                        int xcount = (int)((float)i / (float)m_ConstraintCount);// + (ycount > 0 ? 1 : 0);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount + (cell.x - sz.x) / 2.0f, startPos.y + (cell.y + m_Space.y) * ycount - (cell.y - sz.y) / 2.0f));
                    }
                    break;
                case TextAnchor.MiddleRight:
                    startPos.x = crbPos.x - childSize.x;
                    startPos.y = crbPos.y + (cltPos.y - crbPos.y) / 2.0f - childSize.y / 2.0f;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int ycount = i % m_ConstraintCount;
                        int xcount = (int)((float)i / (float)m_ConstraintCount);// + (ycount > 0 ? 1 : 0);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount + (cell.x - sz.x), startPos.y + (cell.y + m_Space.y) * ycount - (cell.y - sz.y) / 2.0f));
                    }
                    break;
            }

        }
        else
        {
            childSize.x = cell.x * m_ConstraintCount + (m_ConstraintCount - 1) * m_Space.x;
            childSize.y = cell.y * dCount + (dCount - 1) * m_Space.y;

            contentSize.x = childSize.x + m_Padding.left + m_Padding.right;
            contentSize.y = childSize.y + m_Padding.top + m_Padding.bottom;
            content.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, contentSize.x);
            content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, contentSize.y);

            pltPos.x = content.rect.center.x - content.rect.width / 2.0f;
            pltPos.y = content.rect.center.y + content.rect.height / 2.0f;

            prbPos.x = content.rect.center.x + content.rect.width / 2.0f;
            prbPos.y = content.rect.center.y - content.rect.height / 2.0f;

            cltPos.x = pltPos.x + m_Padding.left;
            cltPos.y = pltPos.y - m_Padding.top;

            crbPos.x = prbPos.x - m_Padding.right;
            crbPos.y = prbPos.y + m_Padding.bottom;

            Vector2 startPos = cltPos;
            switch (m_ChildAlignment)
            {
                case TextAnchor.UpperLeft:
                    startPos = cltPos;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        int xcount = i % m_ConstraintCount;
                        int ycount = (int)((float)i / (float)m_ConstraintCount);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount, startPos.y - (cell.y + m_Space.y) * ycount));
                    }
                    break;
                case TextAnchor.UpperCenter:
                    startPos.x = cltPos.x + (crbPos.x - cltPos.x) / 2.0f - childSize.x / 2.0f;
                    startPos.y = cltPos.y;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int xcount = i % m_ConstraintCount;
                        int ycount = (int)((float)i / (float)m_ConstraintCount);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount - (cell.x - sz.x) / 2.0f, startPos.y + (cell.y + m_Space.y) * ycount));
                    }
                    break;
                case TextAnchor.UpperRight:
                    startPos.x = crbPos.x - childSize.x;
                    startPos.y = cltPos.y;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int xcount = i % m_ConstraintCount;
                        int ycount = (int)((float)i / (float)m_ConstraintCount);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount - (cell.x - sz.x), startPos.y + (cell.y + m_Space.y) * ycount));
                    }
                    break;
                case TextAnchor.LowerLeft:
                    startPos.x = cltPos.x;
                    startPos.y = crbPos.y;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int xcount = i % m_ConstraintCount;
                        int ycount = (int)((float)i / (float)m_ConstraintCount);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount, startPos.y + (cell.y + m_Space.y) * ycount - (cell.y - sz.y)));
                    }
                    break;
                case TextAnchor.LowerCenter:
                    startPos.x = cltPos.x + (crbPos.x - cltPos.x) / 2.0f - childSize.x / 2.0f;
                    startPos.y = crbPos.y;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int xcount = i % m_ConstraintCount;
                        int ycount = (int)((float)i / (float)m_ConstraintCount);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount + (cell.x - sz.x) / 2.0f, startPos.y + (cell.y + m_Space.y) * ycount - (cell.y - sz.y)));
                    }
                    break;
                case TextAnchor.LowerRight:
                    startPos.x = crbPos.x - childSize.x;
                    startPos.y = crbPos.y;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int xcount = i % m_ConstraintCount;
                        int ycount = (int)((float)i / (float)m_ConstraintCount);// + (xcount > 0 ? 1 : 0);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount + (cell.x - sz.x), startPos.y + (cell.y + m_Space.y) * ycount - (cell.y - sz.y)));
                    }
                    break;
                case TextAnchor.MiddleLeft:
                    startPos.x = cltPos.x;
                    startPos.y = crbPos.y + (cltPos.y - crbPos.y) / 2.0f - childSize.y / 2.0f;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int xcount = i % m_ConstraintCount;
                        int ycount = (int)((float)i / (float)m_ConstraintCount);// + (xcount > 0 ? 1 : 0);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount, startPos.y - (cell.y + m_Space.y) * ycount - (cell.y - sz.y) / 2.0f));
                    }
                    break;
                case TextAnchor.MiddleCenter:
                    startPos.x = cltPos.x + (crbPos.x - cltPos.x) / 2.0f - childSize.x / 2.0f;
                    startPos.y = crbPos.y + (cltPos.y - crbPos.y) / 2.0f - childSize.y / 2.0f;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int xcount = i % m_ConstraintCount;
                        int ycount = (int)((float)i / (float)m_ConstraintCount); //+ (xcount > 0 ? 1 : 0);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount + (cell.x - sz.x) / 2.0f, startPos.y - (cell.y + m_Space.y) * ycount - (cell.y - sz.y) / 2.0f));
                    }
                    break;
                case TextAnchor.MiddleRight:
                    startPos.x = crbPos.x - childSize.x;
                    startPos.y = crbPos.y + (cltPos.y - crbPos.y) / 2.0f - childSize.y / 2.0f;
                    for (int i = 0; i < items.Length; i++)
                    {
                        ScrollCircleItem item = items[i];
                        Vector2 sz = item.GetSize();
                        int xcount = i % m_ConstraintCount;
                        int ycount = (int)((float)i / (float)m_ConstraintCount);// + (xcount > 0 ? 1 : 0);
                        item.SetPos(new Vector2(startPos.x + (cell.x + m_Space.x) * xcount + (cell.x - sz.x), startPos.y - (cell.y + m_Space.y) * ycount - (cell.y - sz.y) / 2.0f));
                    }
                    break;
            }
        }
    }
}
